<?php
namespace app\home\validate;

use think\Validate;

class DoReg extends Validate
{
    protected $rule = [
        'mobile'           => 'require|number|length:11|checkMobile:mobile',
        'mobile_code'      => 'require|number|length:4',
        'password'         => 'require|confirm:repassword|min:6',
        'repassword'       => 'confirm:password',
        'agree'            => 'require',
    ];

    protected $message = [
        'mobile.require'           => '请输入手机号',
        'mobile.number'            => '手机号格式错误',
        'mobile.length'            => '手机号长度错误',
        'mobile.checkMobile'        => '手机号已存在',
        'mobile_code.require'      => '请输入短信验证码',
        'mobile_code.number'       => '短信验证码格式错误',
        'mobile_code.length'       => '短信验证码长度错误',
        'password.require'         => '请输入密码',
        'password.min'             => '密码长度最少6位',
        'password.confirm'         => '两次输入密码不一致',
        'repassword.confirm'       => '两次输入密码不一致',
        'agree.require'            => '同意服务条款后可注册',
    ];


    /**
     * 手机号是否存在
     * @param int       $mobile     手机号
     * @return bool
     */
    protected function checkMobile($mobile)
    {
        $res =  db('user')          // 通过手机号查找用户
                ->where('mobile',$mobile)
                ->value('id');
        if ($res) {                 // 查询结果
            return false;
        }else{
            return true;
        }
    }
}